package cn.itsource.wechart.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.wechart.domain.BinderDto;
import cn.itsource.wechart.domain.WechartProperties;
import cn.itsource.wechart.service.IWeChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@RestController //配置了这个注解,所有的方法都返回json了,我们需要是跳转(重定向)页面
@Controller
@RequestMapping("/wechart")
public class WeChartController {

    /**
     * 注册微信参数对象,为了拿到yml的参数值
     */
    @Autowired
    private WechartProperties wechartProperties;
    @Autowired
    private IWeChartService weChartService;


    /**
     * 页面点击微信登录访问的接口
     * GET
     * 	http://localhost/wechart/tologin
     * @return
     */
    @GetMapping("/tologin")
    public String tologin() {
        //调用业务层
        return weChartService.toLogin();
    }


    /**
     * 微信自动触发的回调接口
     * @return
     * bugtracker.itsource.cn/wechart/callback?code=091yjt1w3H9h6036Td2w3MWKyj3yjt1n&state=4d9efb03f2d345e38247b849f6a34ff5
     */
    @GetMapping("/callback")
    public String callback(String code) {
        //调用业务层
        return weChartService.callback(code);
    }

    /**
     * POST 微信账号绑定手机号业务
     * 	http://localhost/wechart/binder
     * @return
     * 绑定成功直接去首页,登录成功
     */
    @PostMapping("/binder")
    @ResponseBody //返回json格式
    public AjaxResult binder(@RequestBody BinderDto binderDto) {
        //调用业务层
        return weChartService.binder(binderDto);
        //return null;
    }


    @RequestMapping("/test")
    public String test(){

        System.out.println(wechartProperties);
        System.out.println(wechartProperties.getAppId());
        System.out.println(wechartProperties.getTokenUrl());

        return "redirect:http://www.jd.com";
    }

}
